const baseURL = "https://localhost:44386/";

const Request = (options = {}) => {
	return new Promise((resolve, reject) => {
		//附加上token
		const header = {
			Authorization: uni.getStorageSync("token")
		}
		//header["Authorization"] =2eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJBRCI6IlJvY2tMaWFuZyIsIkVtcGxveWVlX01hc3Rlcl9JRCI6MTAzNjMzODEuMCwiRW1wTm8iOiJZSUw0OCIsIk5hbWUiOiLmooHmtKrpnIciLCJUeXBlIjoiSSIsIkRlcHRObyI6IjEwMDIzODIiLCJHcmFkZSI6IjA4IiwiU2V4IjoiTSIsIk1haWwiOiJyb2NrLmxpYW5nQG1wdC1zb2x1dGlvbi5jb20uY24iLCJFeHQiOiI4MzktMjI2MSIsIlNhbGFyeV9UeXBlIjoiMSIsIk9yZ19JZCI6MjUwLCJEZXB0X1Nob3J0X05hbWUiOiJNUFRLIOizh-ioiuWJteaWsOmDqCDpm7vlrZDllYbli5nomZUiLCJEZXB0X0NoaW5lc2VfTmFtZSI6Ik1QVEsg6LOH6KiK5Ym15paw6YOoIOmbu-WtkOWVhuWLmeiZlSIsIkNvbXBhbnlfQ29kZSI6Ik1QVEsiLCJDb21wYW55X05hbWUiOiLmmIblsbHluILmvKLpgZTnsr7lr4YiLCJMb2dpblR5cGUiOiJBRCIsImlzcyI6IkdldGFjLlNwZWVkIiwiZXhwIjoxNjQ0OTc3NzIxLjIzMDIxNzIsInN1YiI6IkpXVEhlbHBlciIsImF1ZCI6IlVTRVIiLCJpYXQiOiIyMDIyLzIvMTYgMDk6MTU6MjEifQ.blAxtSyU-XBB8qGZ9OtGGLj6Zl4xX9BdZAlJ9uMtaOg";
		uni.request({
			url: baseURL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			timeout:60000,
			header: header,
		}).then(res => {
			//token認證不通過,需要重新登錄
			if (res[1].statusCode == 403) {
				if (res[1].data.indexOf('300')) {
					//認證過期
					uni.showToast({
						icon: "none",
						position: "center",
						title: "登錄過期,請重新登錄",
						success: () => {
							setTimeout(function() {
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}, 1500)
						}
					})
				} else if (res[1].data.indexOf('400')) {
					//非法token
					uni.showToast({
						icon: "none",
						position: "center",
						title: "非法認證請求",
						success: () => {
							setTimeout(function() {
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}, 1500)
						}
					})
				} else if (res[1].data.indexOf('500')) {
					//認證Token未知異常
					uni.showToast({
						icon: "none",
						position: "center",
						title: "未知認證異常,重新登錄",
						success: () => {
							setTimeout(function() {
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}, 1500)
						}
					})
				} else if (res[1].data.indexOf('600')) {
					//Token為空
					uni.showToast({
						icon: "none",
						position: "center",
						title: "未登錄,請登錄",
						success: () => {
							setTimeout(function() {
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}, 1500)
						}
					})
				}
				return false;
			}
			else if(res[1].statusCode==200){
				if(res[1].data.IsSuccess){
					resolve(res[1].data.Data);
				}else{
					uni.showModal({
						content: res[1].data.ErrorMessage,
						showCancel: false
					});
				}
			}
			//請求成功,回掉返回結果
			
		}).catch(error => {
			reject(error)
		})
	});
}

const Post = (url, data, options = {}) => {
	options.method = 'POST';
	options.data = data;
	options.url = url;
	return Request(options)
}

const Get = (url, data, options = {}) => {
	options.method = 'GET';
	options.data = data;
	options.url = url;
	return Request(options)
}

export default {
	Request,
	Get,
	Post
}
